package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.service.wx.WxCouponService;
import com.cskaoyan.util.UserIdUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author chenai
 * @description TODO
 * @date 2022-06-07 21:41
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;

    //mylist?status=0&page=1&limit=10
    @RequestMapping("mylist")
    public BaseRespVo mylist(BaseParam baseParam, Integer status) {
        Integer userId = UserIdUtil.getUserIdFromSubject();
        if (userId == -1) {
            return BaseRespVo.shouldLogin();
        } else {
            return BaseRespVo.ok(wxCouponService.mylist(baseParam, status, userId));
        }

    }

    @RequestMapping("list")
    public BaseRespVo list(BaseParam baseParam) {
        return BaseRespVo.ok(wxCouponService.list(baseParam));
    }

    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody Map<String,String> params) {
        Integer userId = UserIdUtil.getUserIdFromSubject();
        if (userId == -1) {
            return BaseRespVo.shouldLogin();
        } else {
            String couponId = params.get("couponId");
            int cid = Integer.parseInt(couponId);
            return wxCouponService.receive(cid, userId);
        }
    }

    @RequestMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map<String,String> params) {
        Integer userId = UserIdUtil.getUserIdFromSubject();
        if (userId == -1) {
            return BaseRespVo.shouldLogin();
        } else {
            String code = params.get("code");
            return wxCouponService.exchange(userId, code);
        }
    }

    //cartId=833&grouponRulesId=0
    @RequestMapping("selectlist")
    public BaseRespVo selectlist(Integer cartId,Integer groupOnRulesId) {
        Integer userId = UserIdUtil.getUserIdFromSubject();
        if (userId == -1) {
            return BaseRespVo.shouldLogin();
        } else {
            return BaseRespVo.ok(wxCouponService.selectList(userId,cartId,groupOnRulesId));
        }
    }
}
